package karileet.meeting.profile.fragment;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import karileet.meeting.profile.ProfileConf;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.TimePicker;

public abstract class ScheduleDialogFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
	
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Create a new instance of TimePickerDialog and return it
		Date date = new Date(getTime());
		SimpleDateFormat hourformat = new SimpleDateFormat("H", Locale.ENGLISH);
		SimpleDateFormat minuteformat = new SimpleDateFormat("mm", Locale.ENGLISH);
		int hour = Integer.parseInt(hourformat.format(date));
		int minute = Integer.parseInt(minuteformat.format(date));
		TimePickerDialog tpd = new TimePickerDialog(getActivity(), this, hour, minute,
                true);
        return tpd;
	}
	
	public static void startService(long activationTime, Context ctx, Intent intent) 
	{
		PendingIntent pendingAlarmIntent = PendingIntent.getService(ctx, 0, intent, 0);
		
		AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
		           activationTime, AlarmManager.INTERVAL_DAY,
		           pendingAlarmIntent);
	}

	@Override
	public void onTimeSet(TimePicker tp, int h, int m) {
		long activationTime = 0;
		long oneDay = 86400000;
		Calendar AlarmCal = Calendar.getInstance();
		AlarmCal.setTimeInMillis(System.currentTimeMillis());
		AlarmCal.set(Calendar.HOUR_OF_DAY, h);  // set user selection
		AlarmCal.set(Calendar.MINUTE, m);        // set user selection
		AlarmCal.set(Calendar.SECOND, 0);
		//adjust start time, so that service wont start to past time
		if (AlarmCal.getTimeInMillis() < System.currentTimeMillis()) {
			Log.d(ProfileConf.APP, "Padding time one day!");
			activationTime = AlarmCal.getTimeInMillis() + oneDay; 
		} else {
			Log.d(ProfileConf.APP, "Using original time!");
			activationTime = AlarmCal.getTimeInMillis(); 
		}
		setTime(activationTime);
		startService(activationTime, getActivity(), getIntent());
		// Create new fragment and transaction
		Fragment newFragment = new MainListFragment();
		FragmentTransaction transaction = getFragmentManager().beginTransaction();

		// Replace whatever is in the fragment_container view with this fragment,
		// and add the transaction to the back stack
		transaction.replace(android.R.id.content, newFragment);
//		transaction.addToBackStack(null);

		// Commit the transaction
		transaction.commit();
	}

	public abstract void setTime(long time);
	
	public abstract long getTime();
	
	public abstract Intent getIntent();
	
}
